//
//  JYTLoginViewController.m
//  JYT_IOS
//
//  Created by nxa-tankeke on 16/12/15.
//  Copyright © 2016年 nxa-yuanguoxiang. All rights reserved.
//

#import "JYTLoginViewController.h"
#import "JYTLoginHandler.h"
#import "JYTPinPhoneViewController.h"
#import "JYTregisteredViewController.h"
#import <SVProgressHUD.h>

@interface JYTLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textFieldPhone;
@property (weak, nonatomic) IBOutlet UITextField *textFieldPassWord;

@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@end

@implementation JYTLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    NSDictionary *dic = @{@"lftBtnBgImg":@"ic_goback",
                          @"lftBtnTitle":@"no",
                          @"pageTitle":@"登录",
                          @"rhtBtnBgImg":@"no",
                          @"rhtBtnTitle":@"no"};
    
    [self setNavgationBarWithDic:dic];
    

    self.loginBtn.layer.cornerRadius = 5.0;
    [self.loginBtn setBackgroundColor:[UIColor colorWithRed:131/255.0f green:182/255.0f blue:248/255.0f alpha:1]];
   
    
}

- (IBAction)clickforgetBtn:(id)sender {
    JYTPinPhoneViewController *pin = [[JYTPinPhoneViewController alloc]init];
    [self pushViewController:pin];
    
}
- (IBAction)clickBtn:(id)sender {
    
    NSString *userName = self.textFieldPhone.text;
    NSString *password = self.textFieldPassWord.text;
    
 
     [SVProgressHUD showWithStatus:@"正在加载......"];
    //loginWithName:18565761070c andPwd:@"123456"
    [[JYTLoginHandler shared] loginWithName:userName andPwd:password completionHandler:^(BOOL succ) {
        
       
        
        if (succ) {
            [SVProgressHUD dismiss];
            [self goBack];
            //保存账户
            [[NSUserDefaults standardUserDefaults] setObject:userName forKey:@"username"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            [[NSUserDefaults standardUserDefaults] setObject:self.textFieldPassWord.text forKey:@"password"];
        } else {
            
            if (userName.length > 0 && password.length > 0 && password.length < 6) {
                //[self showAlert:@"密码不能少于6位"];
                [SVProgressHUD dismiss];
                [self showMessage:@"密码不能少于6位"];
            } else if(userName.length == 0 || password.length == 0){
                [SVProgressHUD dismiss];
                return ;
            } else{
               // [self showAlert:@"用户名或密码错误"];
                [SVProgressHUD dismiss];
                [self showMessage:@"用户名或密码错误"];
            }
        }
    }];
}
- (IBAction)clickRegistBtn:(id)sender {
    
    JYTregisteredViewController *re = [[JYTregisteredViewController alloc]init];
    [self pushViewController:re];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//弹出提示框
- (void)showAlert:(NSString *) _message{//时间
    UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:nil message:_message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    
    [NSTimer scheduledTimerWithTimeInterval:1.5f
                                     target:self
                                   selector:@selector(timerFireMethod:)
                                   userInfo:promptAlert
                                    repeats:YES];
    [promptAlert show];
}

- (void)timerFireMethod:(NSTimer*)theTimer//弹出框
{
    UIAlertView *promptAlert = (UIAlertView*)[theTimer userInfo];
    [promptAlert dismissWithClickedButtonIndex:0 animated:NO];
    promptAlert = NULL;
}

- (void)showMessage:(NSString *)message
{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    UIView *showview =  [[UIView alloc]init];
    showview.backgroundColor = [UIColor blackColor];
    showview.frame = CGRectMake(1, 1, 1, 1);
    showview.alpha = 1.0f;
    showview.layer.cornerRadius = 5.0f;
    showview.layer.masksToBounds = YES;
    [window addSubview:showview];
    
    UILabel *label = [[UILabel alloc]init];
    label.text = message;
    
    //    CGSize LabelSize = [message sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(290, 9000)];
    //初始化段落，设置段落风格
    
    NSMutableParagraphStyle *paragraphstyle=[[NSMutableParagraphStyle alloc]init];
    paragraphstyle.lineBreakMode=NSLineBreakByCharWrapping;
    
    //设置label的字体和段落风格
    
    NSDictionary *dic=@{NSFontAttributeName:label.font,NSParagraphStyleAttributeName:paragraphstyle.copy};
    
    //计算label的真正大小,其中宽度和高度是由段落字数的多少来确定的，返回实际label的大小
    
    CGRect rect=[label.text boundingRectWithSize:CGSizeMake(290, 9000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    CGSize LabelSize = CGSizeMake(rect.size.width, rect.size.height);
    label.frame = CGRectMake(10, 5, LabelSize.width, LabelSize.height);
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:15];
    [showview addSubview:label];
    showview.frame = CGRectMake((CURRENT_SCREEN_WIDTH - LabelSize.width - 20)/2, CURRENT_SCREEN_HEIGHT - 100, LabelSize.width+20, LabelSize.height+10);
    [UIView animateWithDuration:2 animations:^{
        showview.alpha = 0;
    } completion:^(BOOL finished) {
        [showview removeFromSuperview];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
